#!/bin/sh /etc/rc.common
# Copyright (C) 2017 lean <coolsnowwolf@gmail.com>

START=99

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
white="\033[0m"

dir_file="/usr/share/jd_openwrt_script"

start() {
	clear
	echo "-------------------------------------------"
	echo -e "$green	    jd_openwrt_script$white"
	echo "-------------------------------------------"
	echo -e "$yellow 本插件使用到的脚本$white"
	echo "  1.https://github.com/ITdesk01/JD_Script"
	echo "  2.https://github.com/ITdesk01/Checkjs"
	echo "  3.https://github.com/ITdesk01/AUCloudflareIP"
	echo ""
	echo -e "$yellow 等安装完成以后可以用以下代码调用脚本$white"
	echo -e "$green  sh \$jd $white      #调用JD_Script"
	echo -e "$green  sh \$checkjs $white #调用Checkjs"
	echo -e "$green  sh \$AUCI $white    #调用AUCloudflareIP"
	echo ""
	echo " 脚本用法请到github查看说明"
	echo "-------------------------------------------"
	echo -e "$green 执行脚本安装与检测，请稍等$white" && sleep 5
	#检测网络环境
	ping -c 2 github.com > /dev/null 2>&1
	if [ $? -eq 0 ];then
		echo -e "$green 网络正常$white"
	else
		echo -e "$red 网络连接异常，无法ping通github.com，休息5分钟插件再继续尝试下载脚本$white"
		echo -e "$green 如果5分钟过去还是这样，你可以强制代理github.com ，或者采用底下代码手动安装，看看什么问题 $white"
		echo -e "$green /etc/init.d/jd_openwrt_script stop $white"
		echo -e "$green /etc/init.d/jd_openwrt_script start $white"
		sleep 5m
		start
	fi

	#创建主文件
	if [ -e $dir_file ];then
		echo -e "$green 主文件夹创建完成$white"
	else
		mkdir  $dir_file
	fi
	
	if [ -e $dir_file/script_config ];then
		echo -e "$green script_config创建完成$white"
	else
		mkdir  $dir_file/script_config
	fi

	cd $dir_file

	#开始下载脚本
	if [ -e JD_Script ];then
		echo -e "$green JD_Script已存在$white"
	else
		git clone -b main https://github.com/ITdesk01/JD_Script.git && Detect
		chmod 755 JD_Script/jd.sh
		sh JD_Script/jd.sh update
	fi

	if [ -e Checkjs ];then
		echo -e "$green Checkjs已存在$white"
	else
		git clone https://github.com/ITdesk01/Checkjs.git && Detect
		chmod 755 Checkjs/checkjs.sh
		sh Checkjs/checkjs.sh system_variable
	fi
	
	if [ -e AUCloudflareIP ];then
		echo -e "$green AUCloudflareIP已存在$white"
	else
		git clone https://github.com/ITdesk01/AUCloudflareIP.git && Detect
		chmod 755 AUCloudflareIP/AUCloudflareIP.sh
		chmod 755 AUCloudflareIP/CloudflareST
		sh AUCloudflareIP/AUCloudflareIP.sh system_variable
	fi

	#全局变量生效
	source /etc/profile

	#检测npm依赖(安装时间太久，放到最后)
	npm_install

	#生成备份的时候把script_config一起打包出来
	script_config="/usr/share/jd_openwrt_script/script_config"
	if_sysupgrade=$(grep -o "$script_config" /etc/sysupgrade.conf | wc -l )
	if [ $if_sysupgrade == "0" ];then
		echo "$script_config" >> /etc/sysupgrade.conf
	fi

	exit 0
}

npm_install() {
	npm_if=$(cat $dir_file/npm_install.log | grep "安装完成" |wc -l)
	if [ $npm_if = "1" ];then
		echo -e "$green npm 安装完成$white"
	else
		sh $dir_file/JD_Script/jd.sh npm_install
		echo  "npm 安装完成" > $dir_file/npm_install.log
	fi
}

Detect() {
	if [ $? -eq 0 ]; then
		echo  ""
	else
		echo -e "$red 代码运行失败重新执行$white"
		start
	fi
}


stop() {
	clear
	echo "------------------------------------------"
	echo -e "$yellow 用于删除jd_openwrt_script$white"
	echo "------------------------------------------"

	echo -e "请先保存$green$dir_file/script_config$white，这个也会删除，这个是cookie和其他文件"
	echo ""
	seconds_left=15
	while [ ${seconds_left} -gt 0 ]; do
		echo -n "  ${seconds_left}秒以后开始删除,不清楚的现在crtl+c退出"
		sleep 1
		seconds_left=$(($seconds_left - 1))
		echo -ne "\r"
	done
		echo -e "$green删除计划任务$white"
		sed -i '/JD_Script/d' /etc/crontabs/root >/dev/null 2>&1
		sed -i '/Checkjs/d' /etc/crontabs/root >/dev/null 2>&1
		sed -i '/AUCloudflareIP/d' /etc/crontabs/root >/dev/null 2>&1
		sed -i '/####/d' /etc/crontabs/root >/dev/null 2>&1
		/etc/init.d/cron restart
		sleep 5

		echo -e "$green开始删除全局变量$white"
		sed -i '/AUCloudflareIP/d' /etc/profile >/dev/null 2>&1
		sed -i '/Checkjs/d' /etc/profile >/dev/null 2>&1
		sed -i '/JD_Script/d' /etc/profile >/dev/null 2>&1

		sleep 5

		echo -e "$green删除脚本目录$white"
		if [ -d $dir_file ];then
			rm -rf $dir_file
		fi
		sleep 5

		echo -e "$green删除.ssh目录$white"
		rm -rf /root/.ssh
		sleep 5

		echo ""
		echo "---------------------------------------------------------"
		echo "			删除完成，玩的开心"
		echo -e "彻底关掉插件输$green /etc/init.d/jd_openwrt_script disable$white，这样就不会自己启动了"
		echo "---------------------------------------------------------"
		. /etc/profile

}


